package com.yanqu.road.server.manger.activity.wanyao;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.wanyao.config.*;
import com.yanqu.road.entity.activity.wanyao.entity.*;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.WanYaoBusiness;
import com.yanqu.road.logic.helper.WeightHelper;
import com.yanqu.road.pb.activity.WanYaoProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.wanyao.WanYaoModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class WanYaoMgr extends TempMgr {

    private static WanYaoConfig config;

    private static ActivityInfo activityInfo;

    private static Random random = new Random();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static WanYaoConfig getConfig() {
        return config;
    }

    public static void setConfig(WanYaoConfig config) {
        WanYaoMgr.config = config;
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static void setActivityInfo(ActivityInfo activityInfo) {
        WanYaoMgr.activityInfo = activityInfo;
    }

    public static void reloadActivityData() {
        getLogger().info("reload WanYao activity start");
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.WanYao.getValue());
        if (list.isEmpty()) {
            getLogger().info("no WanYao Activity in show time");
            activityInfo = null;
            config = null;
            return;
        }
        activityInfo = list.get(0);
        //活动配置
        int activityId = activityInfo.getActivityId();
        Map<Integer, Map<Integer, WanYaoTalentConfig>> wanYaoTalentConfigMap = WanYaoBusiness.getWanYaoTalentConfigMap(activityId);
        Map<Integer, WanYaoMissionConfig> wanYaoMissionConfigMap = WanYaoBusiness.getWanYaoMissionConfigMap(activityId);
        Map<Integer, WanYaoMonsterKingConfig> wanYaoMonsterKingConfigMap = WanYaoBusiness.getWanYaoMonsterKingConfigMap(activityId);
        Map<Integer, WanYaoNpcConfig> wanYaoNpcConfigList = WanYaoBusiness.getWanYaoNpcConfigList(activityId);
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityId);

        WanYaoConfig wanYaoConfig = new WanYaoConfig(wanYaoTalentConfigMap, wanYaoMissionConfigMap, wanYaoMonsterKingConfigMap, wanYaoNpcConfigList, activityConfigMap);
        config = wanYaoConfig;
        //引导配置

        getLogger().info("reload wan yao end, activityId {}.", activityInfo.getActivityId());
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(WanYaoModule.class).syncConfig();
            player.getModule(WanYaoModule.class).syncUserData();

        }
    }

    public static int getOriginalPower(){
        if (config == null) {
            return 20;
        }
        return config.getPowerInit();
    }


    public static WanYaoProto.WanYaoConfigSync.Builder getConfigMsg(String language) {
        if (config == null) {
            return null;
        }
        WanYaoProto.WanYaoConfigSync.Builder builder = WanYaoProto.WanYaoConfigSync.newBuilder();
        for (Map<Integer, WanYaoTalentConfig> map : config.getWanYaoTalentConfigMap().values()) {
            for (WanYaoTalentConfig wanYaoTalentConfig : map.values()) {
                WanYaoProto.WanYaoTalentConfig.Builder talentBuild = WanYaoProto.WanYaoTalentConfig.newBuilder();
                talentBuild.setId(wanYaoTalentConfig.getId());
                talentBuild.setType(wanYaoTalentConfig.getType());
                talentBuild.setLevel(wanYaoTalentConfig.getLevel());
                talentBuild.setUnlock(wanYaoTalentConfig.getUnlock());
                talentBuild.setCost(wanYaoTalentConfig.getCost());
                talentBuild.setName(ServerLanguageMgr.getContent(wanYaoTalentConfig.getName(), language));
                talentBuild.setDesc(ServerLanguageMgr.getContent(wanYaoTalentConfig.getDesc(), language));
                talentBuild.setTalentPara(wanYaoTalentConfig.getTalentPara());
                talentBuild.setDisplayText(ServerLanguageMgr.getContent(wanYaoTalentConfig.getDisplayText(), language));
                builder.addTalentConfig(talentBuild);
            }
        }

        for (WanYaoMonsterKingConfig kingConfig : config.getWanYaoMonsterKingConfigMap().values()) {
            WanYaoProto.WanYaoMonsterKingConfig.Builder kingBuilder = WanYaoProto.WanYaoMonsterKingConfig.newBuilder();
            kingBuilder.setLevel(kingConfig.getLevel());
            kingBuilder.setCost(kingConfig.getCost());
            kingBuilder.setXiaoYaoWeights(kingConfig.getMonsterWeight());
            kingBuilder.setXiaoYaoMax(kingConfig.getXiaoYaoMax());
            kingBuilder.setStepCountMax(kingConfig.getStepCountMax());
            kingBuilder.setEnergyCost(kingConfig.getEnergyCost());
            kingBuilder.setXiaoYaoMin(kingConfig.getXiaoYaoMin());
            builder.addMonsterKingConfig(kingBuilder);
        }

        for (WanYaoMissionConfig missionConfig : config.getWanYaoMissionConfigMap().values()) {
            WanYaoProto.WanYaoMissionConfig.Builder missionBuilder = WanYaoProto.WanYaoMissionConfig.newBuilder();
            missionBuilder.setId(missionConfig.getId());
            missionBuilder.setName(ServerLanguageMgr.getContent(missionConfig.getName(), language));
            missionBuilder.setMap(missionConfig.getMapId());
            missionBuilder.setEnemyList(missionConfig.getEnemyList());
            missionBuilder.setEnemyPower(missionConfig.getEnemyPower());
            missionBuilder.setBossId(missionConfig.getBossId());
            missionBuilder.setBossPower(missionConfig.getBossPower());
            missionBuilder.setEnemyBean(missionConfig.getEnemyBean());
            missionBuilder.setBeanList(missionConfig.getBeanList());
            missionBuilder.setRewards(missionConfig.getRewards());
            missionBuilder.setBossBean(missionConfig.getBossBean());
            builder.addMissionConfig(missionBuilder);
        }

        for (WanYaoNpcConfig npcConfig : config.getGetWanYaoNpcConfigMap().values()) {
            WanYaoProto.WanYaoArmyConfig.Builder npcBuilder = WanYaoProto.WanYaoArmyConfig.newBuilder();
            npcBuilder.setId(npcConfig.getId());
            npcBuilder.setIcon(npcConfig.getIcon());
            npcBuilder.setName(ServerLanguageMgr.getContent(npcConfig.getName(), language));
            npcBuilder.setDesc(ServerLanguageMgr.getContent(npcConfig.getDesc(), language));
            npcBuilder.setAtk(npcConfig.getAtk());
            builder.addNpcConfig(npcBuilder);
        }
        return builder;
    }


    /**
     * 检测最多杀多少个
     */
    public static int checkMostKill(long bestAtk) {
        int bestKill = 0;

        for (WanYaoMissionConfig wanYaoMissionConfig : config.getWanYaoMissionConfigMap().values()) {
            List<Long> enemyPowerList = wanYaoMissionConfig.getEnemyPowerList();
            for (long atk : enemyPowerList) {
                if (bestAtk < atk) {
                    return bestKill;
                } else {
                    bestKill++;
                    bestAtk = bestAtk - atk;
                }
            }
            if (bestAtk < wanYaoMissionConfig.getBossPower()) {
                return bestKill;
            } else {
                bestKill++;
                bestAtk = bestAtk - wanYaoMissionConfig.getBossPower();
            }
        }
        return bestKill;

    }

    /**
     * 获取加成后的妖怪atk
     */
    public static long getMonsterBuffedAtk(int level, int addRate) {
        if (config == null) {
            return 0;
        }
        WanYaoNpcConfig npcConfig = config.getGetWanYaoNpcConfigMap().get(level);
        if (npcConfig == null) {
            getLogger().error("万妖行获取妖怪配置失败,activityId={},level={}", activityInfo.getActivityId(), level);
        }
        int atk = npcConfig.getAtk();
        atk = atk + atk * addRate / 1000;

        return atk;
    }

    /**
     * 根据等级获取盘面妖怪配置
     */
    public static WanYaoNpcConfig getWanYaoNpcConfigByLevel(int level) {
        if (config == null) {
            return null;
        }
        return config.getGetWanYaoNpcConfigMap().get(level);
    }

    /**
     * 用妖王等级获取最大步数
     *
     * @param kingLevel
     * @return
     */

    public static int getMaxStepByLevel(int kingLevel) {
        if (config == null) {
            return 0;
        }
        WanYaoMonsterKingConfig kingConfig = config.getWanYaoMonsterKingConfigMap().get(kingLevel);
        if (kingConfig == null) {
            return 0;
        }
        return kingConfig.getStepCountMax();
    }

//    /**
//     * 获取玩家初始buff
//     * @return
//     */
//
//    public static Map<Integer, WanYaoBuffData> getInitBuffMap(){
//        if (config == null){
//            return null;
//        }
//        Map<Integer, WanYaoBuffData> buffMap = new ConcurrentHashMap<>();
//        for (Map<Integer, WanYaoTalentConfig> talentConfig:config.getWanYaoTalentConfigMap().values()){
//            WanYaoTalentConfig wanYaoTalentConfig = talentConfig.get(1);
//            if (wanYaoTalentConfig == null){
//                continue;
//            }
//            WanYaoBuffData buffData = new WanYaoBuffData();
//            buffData.setBuffId(wanYaoTalentConfig.getId());
//            buffData.setBuffLevel(1);
//            buffData.setBuffTime(0);
//            buffMap.put(wanYaoTalentConfig.getType(),buffData);
//        }
//        if (buffMap.isEmpty()){
//            return null;
//        }
//        return buffMap;
//    }

    /**
     * 获取初始化的玩家数据
     *
     * @return
     */

    public static WanYaoLevelData getInitWanYaoLevelData() {
        WanYaoLevelData wanYaoLevelData = new WanYaoLevelData();
        wanYaoLevelData.setLevel(0);
        wanYaoLevelData.setEnemyIndex(0);
        wanYaoLevelData.setRemainBlood(0);
        return wanYaoLevelData;
    }

    /**
     * 获取初始化的棋盘数据
     */

    public static Map<Integer, WanYaoBlockData> getInitBoardData(int step) {
        if (config == null) {
            return null;
        }
        return config.getInitGuideBoard(step);
    }

    /**
     * 用妖王等级获取当前盘面上可以存在的最高等级
     */
    public static int getMaxBlockLevelByKingLevel(int kingLevel) {
        if (config == null) {
            return 0;
        }
        WanYaoMonsterKingConfig kingConfig = config.getWanYaoMonsterKingConfigMap().get(kingLevel);
        if (kingConfig == null) {
            return 0;
        }
        return kingConfig.getXiaoYaoMax();
    }


    /**
     * 根据妖王等级获取随机的方块
     *
     * @param kingLevel
     * @param blockId
     * @param x
     * @param y
     * @return
     */
    public static WanYaoBlockData getRandomBlockByKingLevel(int kingLevel, int blockId, int x, int y) {
        WanYaoBlockData blockData = new WanYaoBlockData();
        blockData.setBlockId(blockId);
        blockData.setX(x);
        blockData.setY(y);
        int randomBlockLevel = getRandomBlockLevel(kingLevel);
        blockData.setBlockLevel(randomBlockLevel);
        return blockData;

    }

    /**
     * 根据妖王等级获取随机的方块等级
     *
     * @param kingLevel
     * @return
     */

    public static int getRandomBlockLevel(int kingLevel) {
        WanYaoMonsterKingConfig kingConfig = getWanYaoMonsterKingConfigByLevel(kingLevel);
        if (kingConfig == null) {
            getLogger().error("万妖行id{}妖王等级配置不存在，妖王等级为{}", activityInfo.getActivityId(), kingLevel);
            return random.nextInt(4) + 1;
        }

        NormalWeightItem randomResult = WeightHelper.getRandomResult(kingConfig.getMonsterWeightList());
        if (randomResult == null) {
            getLogger().error("万妖行id{}妖王等级配置不存在，妖王等级为{}", activityInfo.getActivityId(), kingLevel);
            return random.nextInt(4) + 1;
        }
        int level = randomResult.getValue().intValue();
        return level;
    }

    /**
     * 根据妖王获取妖王配置
     *
     * @return
     */
    public static WanYaoMonsterKingConfig getWanYaoMonsterKingConfigByLevel(int kingLevel) {
        if (config == null) {
            return null;
        }
        return config.getWanYaoMonsterKingConfigMap().get(kingLevel);
    }

    /**
     * 根据类型 id获取天赋
     *
     * @return
     */
    public static WanYaoTalentConfig getWanYaoTalentConfigById(int id) {
        if (config == null) {
            return null;
        }


        for (Map<Integer, WanYaoTalentConfig> talentConfigMap : config.getWanYaoTalentConfigMap().values()) {
            for (WanYaoTalentConfig talentConfig : talentConfigMap.values()) {
                if (talentConfig.getId() == id) {
                    return talentConfig;
                }
            }
        }

        return null;
    }

    /**
     * 根据类型 等级获取天赋
     *
     * @return
     */

    public static WanYaoTalentConfig getWanYaoTalentConfigByLevel(int type, int level) {
        if (config == null) {
            return null;
        }
        return config.getWanYaoTalentConfigMap().get(type).get(level);
    }

    /**
     * 根据类型获取天赋列表
     *
     * @return
     */
    public static Map<Integer, WanYaoTalentConfig> getWanYaoTalentConfigMapByType(int type) {
        if (config == null) {
            return null;
        }
        return config.getWanYaoTalentConfigMap().get(type);
    }


    /**
     * 用xy获取方块
     *
     * @param blockDataMap
     * @param x
     * @param y
     * @return
     */


    public static WanYaoBlockData getBlockByXY(Map<Integer, WanYaoBlockData> blockDataMap, int x, int y) {
        for (WanYaoBlockData blockData : blockDataMap.values()) {
            if (blockData.getX() == x && blockData.getY() == y) {
                return blockData;
            }
        }
        return null;
    }

    /**
     * 解析方块信息
     *
     * @param wanYaoBlockData
     * @return
     */

    public static WanYaoProto.WanYaoBlockMsg.Builder parseBlockMsg(WanYaoBlockData wanYaoBlockData) {
        WanYaoProto.WanYaoBlockMsg.Builder blockBuilder = WanYaoProto.WanYaoBlockMsg.newBuilder();
        blockBuilder.setId(wanYaoBlockData.getBlockId());
        blockBuilder.setLevel(wanYaoBlockData.getBlockLevel());
        blockBuilder.setX(wanYaoBlockData.getX());
        blockBuilder.setY(wanYaoBlockData.getY());
        return blockBuilder;
    }

    /**
     * 解析棋盘信息
     *
     * @param boardData
     * @return
     */

    public static WanYaoProto.WanYaoBoardMsg.Builder parseBoardMsg(Map<Integer, WanYaoBlockData> boardData) {
        WanYaoProto.WanYaoBoardMsg.Builder boardBuilder = WanYaoProto.WanYaoBoardMsg.newBuilder();
        for (WanYaoBlockData wanYaoBlockData : boardData.values()) {
            WanYaoProto.WanYaoBlockMsg.Builder blockBuilder = parseBlockMsg(wanYaoBlockData);
            boardBuilder.addBlocks(blockBuilder);
        }
        return boardBuilder;

    }

    @Override
    public boolean stop() {
        return true;
    }
}
